package cn.wizzer.app.web.commons.utils;

import cn.hutool.crypto.SecureUtil;
import cn.wizzer.framework.util.DateUtil;
import cn.wizzer.framework.util.HttpUtil;
import cn.wizzer.framework.util.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import org.nutz.lang.Times;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;

public class SMSUtil {

    private static final Log log = Logs.get();

    private static String URL = "https://api.paasoo.com/json?";

    private static String KEY = "qv1w1zfh";

    private static String API_SECRET = "6Qj5xGbk";

    private static String CHM_URL = "http://sms.skylinelabs.cc:20003/sendsmsV2?";

    private static String CHM_ACCOUNT = "cs_oikvop";

    private static String CHM_PWD = "h46LAuJF";


    /**
     * Example
     * <p>
     * https://api.paasoo.com/json?key=API_KEY&secret=API_SECRET&from=SMS&to=8615800000000&text=Hello+world
     *
     * @param phone   发送目标号码，格式为国家区号直接接手机号码，如8618812345678，86为中国区号，18812345678为手机号码
     * @param context 发送内容 eg: verification code  [345645]
     * @return
     */

    public static SmsSendResult send(String phone, String context) {
        SmsSendResult result = new SmsSendResult();
        try {
            String sendUrl = URL + "key=" + KEY + "&secret=" + API_SECRET + "&from=SMSB" + "&to=" + phone + "&text="
                    + URLEncoder.encode("【BallGold】" + context, Charsets.UTF_8.displayName());

            log.info("发送短信验证码URL：" + sendUrl);

            String body = HttpUtil.createRequest()
                    .setUrl(sendUrl)
                    .setHeader("Content-Type", "application/x-www-form-urlencoded")
                    .send()
                    .getBodyAndClose();
            JSONObject jsonObject = JSONUtil.parseToJSON(body);
            result.setStatus(jsonObject.getString("status"));
            result.setStatusCode(jsonObject.getString("status_code"));
            result.setMessageid(jsonObject.getString("messageid"));
            log.info("发送短信验证码 result:" + result + ",手机号：" + phone);
            return result;
        } catch (IOException e) {
        }
        return result;
    }


    public static SmsCHMResult sendCHM(String phone, String context) {
        SmsCHMResult result = new SmsCHMResult();
        try {
            String datetime = Times.format("yyyyMMddHHmmss", new Date());
            String sign = SecureUtil.md5(CHM_ACCOUNT + CHM_PWD + datetime);
            String sendUrl = CHM_URL + "account=" + CHM_ACCOUNT + "&sign=" + sign + "&datetime=" + datetime + "&numbers=" + phone + "&content="
                    + URLEncoder.encode("Your SMS verification code is:" + context, Charsets.UTF_8.displayName());
            log.info("CHM发送短信验证码URL：" + sendUrl);

            String body = HttpUtil.createRequest()
                    .setUrl(sendUrl)
                    .setHeader("Content-Type", "application/x-www-form-urlencoded")
                    .send()
                    .getBodyAndClose();
            JSONObject jsonObject = JSONUtil.parseToJSON(body);
            result.setStatus(jsonObject.getIntValue("status"));
            result.setSuccess(jsonObject.getIntValue("success"));
            result.setFail(jsonObject.getIntValue("fail"));
            result.setArray(jsonObject.getJSONArray("array"));
            log.info("CHM发送短信验证码 result:" + result + ",手机号：" + phone);
            return result;
        } catch (IOException e) {
            log.error("CHM发送短信验证码IO异常");
        }
        return result;
    }




    public static void main(String[] args) {
        SmsCHMResult result = sendCHM("66826681625", "456789");
        System.out.println(result);
    }

}
